import { createStore } from 'vuex'
const state = {
  coures: [],         // 学科目录
  couresStartTime: new DataTransfer(),
  paperMenuWithDateJoint: [],
  paperMenuWithDateJointStartTime: new DataTransfer(),
  paperMenuWithDateGenarel: [],
  paperMenuWithDateGenarelStartTime: new DataTransfer(),
  knowledgepointsMenu: [],
  knowledgepointsMenuStartTime: new Date(),
  name:'全局的name属性',
  selectQuestionList:[],
  paperquestionList:[],   // paperquestionList的全局状态
  question: Object,
  paper: Object,
  genaralPaper: Object,
  index: -1,
  selcetKnowledgepointList:[],
  selectedPaperId: -1,
  importQuestionType: "importQuestionsAndPaper",
  nodeId: 1,
  isPrivate: false,           //是否为私有试题
}

const getters = {
   // 获取课程
   getCoures(state){
    return state.coures
  },
  // 获取课程的有效期24小时内，返回值未过期则为true
  getCouresExpiration(state){
    const currentDate = new Date();
    const timeDiffInMilliseconds = Math.abs(currentDate - state.couresStartTime);
    const hoursDiff = timeDiffInMilliseconds / (1000 * 60 * 60);
    return hoursDiff < 24
  },
  // 获取试卷日期（联考）侧边目录菜单
  getPaperMenuWithDateJoint(state){
    return state.paperMenuWithDateJoint
  },
  // 获取试卷日期（联考）侧边目录菜单的有效期
  getPaperMenuWithDateJointExpiration(state){
    const currentDate = new Date();
    const timeDiffInMilliseconds = Math.abs(currentDate - state.paperMenuWithDateJoint);
    const hoursDiff = timeDiffInMilliseconds / (1000 * 60 * 60);
    return hoursDiff < 24
  },
  // 获取试卷日期（非联考）侧边目录菜单
  getPaperMenuWithDateGenarel(state){
    return state.paperMenuWithDateGenarel
  },
  // 获取试卷日期（非联考）侧边目录菜单的有效期
  getPaperMenuWithDateGenarelExpiration(state){
    const currentDate = new Date();
    const timeDiffInMilliseconds = Math.abs(currentDate - state.paperMenuWithDateGenarel);
    const hoursDiff = timeDiffInMilliseconds / (1000 * 60 * 60);
    return hoursDiff < 24
  },
  // 获取知识点侧边目录菜单
  getKnowledgePointMenuCache(state){
    return state.knowledgepointsMenu
  },
  // 获取知识点侧边目录菜单的有效期
  getKnowledgePointMenuExpiration(state){
    const currentDate = new Date();
    const timeDiffInMilliseconds = Math.abs(currentDate - state.knowledgepointsMenuStartTime);
    const hoursDiff = timeDiffInMilliseconds / (1000 * 60 * 60);
    return hoursDiff < 24
  },
  getName(state){
    return state.name
  },
  getQuestionListValue(state){
    return state.paperquestionList
  },
  getNodeId(state){
    return state.nodeId
  },
  getSelectQuestionList(state){
    return state.selectQuestionList
  },
  getIsPrivate(state){
    return state.isPrivate
  },
}
const mutations = {
  // 设置课程缓存，并设置其开始时间
  setCoures(state, value_list){
    state.coures = value_list
    let currentTime = new Date()
    state.couresStartTime = currentTime
  },
  // 设置联考卷缓存，并设置其开始时间
  setPaperMenuWithDateJoint(state, value_list){
    state.paperMenuWithDateJoint = value_list
  },
  // 设置非联考卷缓存，并设置其开始时间
  setPaperMenuWithDateGenarel(state, value_list){
    state.paperMenuWithDateGenarel = value_list
  },
  // 设置侧边知识点缓存，并设置其开始时间
  setKnowledgepointsMenu(state, value_list){
    state.knowledgepointsMenu = value_list
  },
  setIsPrivate(state, value){
    state.isPrivate = value
  },
  setPaper(state, value){
    state.paper = value
  },
  setGenaralPaper(state, value){    
    state.paper = value
  },
  setPaperId(state, value){
    state.selectedPaperId = value
  },
  setQuestion(state, value){    
    state.question = value
  },
  setName(state,name){
    state.name = name
  },
  setIndex(state, index){
    state.index = index
  },
  setSelectQuestionList(state,questionListValue){
    state.selectQuestionList = questionListValue.slice(0)
    console.log("我是全局，收到题目：",state.selectQuestionList)
  },
  // 初始化题目列表或者整体覆盖更新，接收整个题目列表
  setQuestionListValue(state, questionListValue){
    // console.log("全局收到题目列表：", questionListValue)
    state.paperquestionList = questionListValue.slice(0)
  },
  // 设置知识点的id
  setQuestionListKnowledgepoint(state, value){
    state.paperquestionList[value.id].knowledgepoint=value.value_id
    state.paperquestionList[value.id].knowledgepoint_cn=value.value_name
  },
  //设置题目插图
  setQuestionListPicture(state, value){
    if(state.paperquestionList[value.id].pictures==""){
      state.paperquestionList[value.id].pictures=value.pictures
    }else{
      state.paperquestionList[value.id].pictures+="-"+value.pictures
    }       
  },
  setSelcetKnowledgepointList(state, value_list){
    state.selcetKnowledgepointList = value_list
  },
  setNodeId(state, value){
    state.nodeId = value
  },
}
const actions = {

}
const modules = {

}
const store = createStore({
  state,
  getters,
  mutations,
  actions,
  modules,
  strict:true
})
export default store